public class Stack {

    private class Node {
        int value;
        Node next;

        Node() {}
        Node(int val) {
            this.value = val;
            this.next = null;
        }
    }

    private Node top;
    private Node head;
    private int len;
    private int size;

    public Stack(int size) {
        this.size = size;
        this.len = 0;
    }

    public void push(int val) {
        if (this.len >= this.size) {
            System.out.println("warning: 栈满不可添加");
            return;
        }
        Node node = new Node(val);
        if (this.len == 0) {
            this.top = node;
            this.head = node;
        } else {
            this.top.next = node;
            this.top = node;
            this.top.next = null;
        }
        this.len ++;
    }

    public Node pop() {
        Node node;
        if (this.len == 0) {
            System.out.println("warning: 栈内无可pop的元素");
            return null;
        } else if (this.len == 1) {
            node = this.top;
            this.top = null;
            this.head = null;
        } else {
            node = this.top;
            this.top = this.traverse();
        }
        this.len --;
        return node;
    }

    public Node traverse() {
        Node node = this.head;
        for (int i = 1; i < this.len-1; i++) {
            node = node.next;
        }
        return node;
    }

    public void show() {
        Node node = this.head;
        for (int i = 0; i < this.len; i++) {
            System.out.printf("-%d-", node.value);
            node = node.next;
        }
        System.out.println();
    }

    public int len() {
        return this.len;
    }

    public static void main(String[] args) {
        System.out.println("stack...");
        int[] stack_list = {1, 3, 5, 2, 4, 6, 7, 8, 9, 1, 5, 6};
        Stack stack = new Stack(10);
        for (int i = 0; i < stack_list.length; i ++) {
            stack.push(stack_list[i]);
        }
        stack.show();
        stack.pop();
        stack.pop();

        stack.show();
    }
}
